package com.demo.project_template.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.demo.project_template.entity.SysUser;
import com.demo.project_template.entity.SysUserRole;
import com.demo.project_template.mapper.SysUserMapper;
import com.demo.project_template.service.SysUserRoleService;
import com.demo.project_template.service.SysUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.demo.project_template.util.CheckEmptyUtil;
import com.demo.project_template.util.JwtUtil;
import com.demo.project_template.vo.request.sys.SysUserRequestVO;
import org.springframework.beans.BeanUtils;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author 杨玉龙
 * @since 2024-01-03 20:32:03
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {
    @Resource
    private BCryptPasswordEncoder passwordEncoder;
    @Resource
    private SysUserRoleService sysUserRoleService;

    @Override
    public void deleteSysUser(Integer sysUserId) {
        //todo 删除后台用户附带的其他数据
        removeById(sysUserId);
    }

    @Override
    @Transactional
    public void createOrUpdateSysUser(Integer updateSource,SysUserRequestVO sysUserRequestVO) {
        SysUser sysUser = new SysUser();
        BeanUtils.copyProperties(sysUser,sysUserRequestVO);
        sysUser.setPassword(passwordEncoder.encode(sysUserRequestVO.getPassword()));
        Long userid = JwtUtil.getUserId();
        if (CheckEmptyUtil.isEmpty(sysUserRequestVO.getId())){
            sysUser.setCreateBy(userid);
        }
        sysUser.setUpdateBy(userid);
        saveOrUpdate(sysUser);
        if (updateSource.equals(0)){
            return;
        }
        //新增用户与角色管理
        if (!CheckEmptyUtil.isEmpty(sysUserRequestVO.getId())){
            sysUserRoleService.remove(new LambdaUpdateWrapper<SysUserRole>().eq(SysUserRole::getUserId,sysUserRequestVO.getId()));
        }
        List<SysUserRole> userRoles = new ArrayList<>();
        sysUserRequestVO.getRoleIds().forEach(roleId->{
            userRoles.add(new SysUserRole(sysUser.getId(),roleId));
        });
        sysUserRoleService.saveBatch(userRoles);
    }

    @Override
    public boolean checkUserNameUnique(Long userId,String username) {
        userId = userId == null ? -1L : userId;
        SysUser info = getOne(new LambdaQueryWrapper<SysUser>().eq(SysUser::getUsername, username));
        return !CheckEmptyUtil.isEmpty(info) || info.getId().equals(userId);
    }

    @Override
    public boolean checkPhoneUnique(Long userId, String tel) {
        userId = userId == null ? -1L : userId;
        SysUser info = getOne(new LambdaQueryWrapper<SysUser>().eq(SysUser::getPhoneNumber, tel));
        return !CheckEmptyUtil.isEmpty(info) || info.getId().equals(userId);
    }
}
